local skel = fk.CreateSkill {
  name = "lb__liewang",
  tags = { Skill.Spirited },
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb__liewang"] = "冽妄",
  [":lb__liewang"] = "<a href='#SpiritedSkillDesc'><b>昂扬技</b></a>，" ..
      "出牌阶段，你可亮出牌堆顶一张牌，用一张牌交换之。" ..
      --"出牌阶段，你可亮出牌堆顶三张牌，用一张牌交换其中一张。" ..
      "<a href='#SpiritedSkillDesc'>激昂</a>：本回合弃牌堆中的花色被补齐时，你视为使用一张改为冰冻伤害的【万箭齐发】，然后将本回合进入弃牌堆中的所有牌置于牌堆底。",

  ["@[suits]lb__liewang"] = "冽妄",
  ["#lb__liewang"] = "冽妄：你可亮出牌堆顶一张牌，用一张牌交换之",
  ["#lb__liewang_change"] = "冽妄：请用一张牌交换%arg",
  --[[
  ["#lb__liewang"] = "冽妄：你可以亮出牌堆顶三张牌，用一张牌交换其中一张",
  ["#lb__liewang_ask"] = "冽妄：请用一张牌交换其中一张",
  ["#lb__liewang_give"] = "冽妄：你可以将至多两张牌分配给任意角色（剩余%arg张）",
  --]]
  ["$lb__liewang1"] = "你们的失败早已注定。",
  ["$lb__liewang2"] = "在我的威严之下颤抖吧！",
  ["$lb__liewang3"] = "寒潮将埋葬你们！",
  ["$lb__liewang4"] = "冰寒的审判！",
}

Fk:addPoxiMethod {
  name = skel.name,
  card_filter = function(to_select, selected, data, extra_data)
    local ids = {}
    for _, flag in ipairs(data) do
      if table.find(flag[2], function(id)
            return table.contains(selected, id)
          end) then
        table.insertTable(ids, flag[2])
      end
    end
    return #selected < 2 and not table.contains(ids, to_select)
  end,
  feasible = function(selected, data, extra_data)
    return #selected == 2
  end,
  default_choice = function(data, extra_data)
    return { data[1][2][1], data[2][2][1] }
  end,
  post_select = function(selected, data, extra_data)
    local ids = {} --排序
    for _, flag in ipairs(data) do
      for _, id in ipairs(flag[2]) do
        if table.contains(selected, id) then
          table.insert(ids, id)
        end
      end
    end
    return ids
  end,
  prompt = "#lb__liewang_ask",
}

skel:addEffect("active", {
  audio_index = {1, 2},
  anim_type = "drawcard",
  prompt = "#lb__liewang",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:getMark(skel.name) == 0 --and player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    room:setPlayerMark(player, skel.name, 1)
    local cards = room:getNCards(1)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true, player)
    local ids = player:getCardIds("he")
    if #ids > 0 then
      --[[
      local change = room:askToPoxi(player, {
        poxi_type = skel.name,
        cancelable = false,
        data = {
          { skel.name,             cards },
          { player:__touistring(), ids },
        },
      })
      local hand = table.remove(cards, table.indexOf(cards, table.find(cards, function(id)
        return table.contains(change, id)
      end)))
      room:moveCardTo(change, Card.Processing, nil, fk.ReasonExchange, skel.name, nil, true, player)
      room:obtainCard(player, hand, true, fk.ReasonExchange, player, skel.name)
      --]]
      if #ids > 1 then
        ids = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          cancelable = false,
          include_equip = true,
          prompt = "#lb__liewang_change:::".. Fk:getCardById(cards[1]):toLogString()
        })
        room:moveCardTo(ids, Card.Processing, nil, fk.ReasonExchange, skel.name, nil, true, player)
        room:obtainCard(player, cards, true, fk.ReasonExchange, player, skel.name)
      end
    end
    room:cleanProcessingArea()
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  anim_type = "offensive",
  audio_index = {3, 4},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(skel.name, true) and player:getMark(skel.name) == 1 and not room:getBanner("#CenterSuitComplete") then
      local suits = {}
      for _, id in ipairs(DIY.getCenterCards(room)) do
        local suit = Fk:getCardById(id).suit
        if suit ~= Card.NoSuit then
          table.insertIfNeed(suits, suit)
        end
      end
      return #suits == 4
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, skel.name, 0)
    room:useVirtualCard("archery_attack", nil, player, room:getOtherPlayers(player), skel.name, nil,
      { ChangeDamageType = fk.IceDamage })
    --[[  第一版，分配两张牌
    local num = math.min(#DIY.getCenterCards(room), 2)
    while not player.dead and #DIY.getCenterCards(room) > 0 and num > 0 do
      local tos, cards = room:askToChooseCardsAndPlayers(player, {
        min_num = 1,
        max_num = 1,
        min_card_num = 1,
        max_card_num = num,
        targets = room.alive_players,
        pattern = tostring(Exppattern { id = DIY.getCenterCards(room) }),
        skill_name = skel.name,
        prompt = "#lb__liewang_give:::" .. num,
        cancelable = true,
        expand_pile = DIY.getCenterCards(room),
      })
      if #tos > 0 and #cards > 0 then
        num = num - #cards
        room:moveCardTo(cards, Card.PlayerHand, tos[1], fk.ReasonGive, skel.name, nil, true, player)
      else
        return
      end
    end
    --]]
    local cards = DIY.getCenterCards(room)
    if #cards > 0 then
      room:moveCards({
        ids = cards,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        proposer = player,
        skillName = skel.name,
        moveVisible = true,
        drawPilePosition = -1,
      })
      cards = table.filter(cards, function(id)
        return room:getCardArea(id) == Card.DrawPile
      end)
      if #cards > 0 then
        room:sendLog {
          type = "#PutCardBottom",
          card = cards,
        }
      end
    end
  end,
})


skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

skel:addLoseEffect(function(self, player)
  player.room:setPlayerMark(player, "@lb__liewang", 0)
end)

return skel